<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>lifestyle</title>
</head>

<body>
  <div id="app">
    x: {{ position.x }} <br>
    y: {{ position.y }}
  </div>
  <script type="module">
    import { createApp, reactive, onMounted, onUnmounted } from './node_modules/vue/dist/vue.esm-browser.js'

    const app = createApp({
      setup () {
        // 第一个参数 props
        // 第二个参数 context，attrs、emit、slots
        // 创建响应式对象（返回proxy对象）
        const position = reactive({
          x: 0,
          y: 0
        })

        const update = e => {
          position.x = e.pageX
          position.y = e.pageY
        }

        onMounted(() => {
          window.addEventListener('mousemove', update)
        })
        // 相当于destroyed
        onUnmounted(() => {
          window.removeEventListener('mousemove', update)
        })

        return {
          position
        }
      }
      // mounted() {
      //   const update = e => {
      //     this.position.x = e.pageX
      //     this.position.y = e.pageY
      //   }
      //   window.addEventListener('mousemove', update)
      // }
    })
    // 打印查看app
    console.log(app)

    app.mount('#app')
  </script>
</body>

</html>